package com.zero.insp.controller;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.zero.insp.common.PPageInfo;
import com.zero.insp.common.PResult;
import com.zero.insp.domain.TCompanyExh;
import com.zero.insp.domain.TCompanyExhExample;
import com.zero.insp.domain.TUserInfo;
import com.zero.insp.domain.TUserInfoExample;
import com.zero.insp.services.InspService;
import com.zero.insp.services.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @author : pengjie
 * @PackageName : com.zero.insp.controller
 * @Description : 查询条件参数提供类
 * @email : 627799251@qq.com
 * @Date : 2019/3/27 9:47
 */
@Api(value="查询条件公共请求接口")
@RestController
public class WebQueryParamController {

    @Autowired
    private InspService inspService;
    @Autowired
    private UserService userService;

    private static LoadingCache<String, Set<String>> cache = null;

    static {
        cache = CacheBuilder.newBuilder().expireAfterWrite(5, TimeUnit.MINUTES)
                .build(new CacheLoader<String, Set<String>>() {

                    @Override
                    public Set<String> load(String key) throws Exception {
                        return new HashSet<>();
                    }
                });
    }
    @ApiOperation("查询条件-用户类型列表")
    @GetMapping("/com/v1/utype")
    public PResult<Set<String>> usertype() {
        Set<String> set = new HashSet<String>();
        set.add("高级用户");
        set.add("巡检用户");
        return new PResult<>(set);
    }
    @ApiOperation("查询条件-馆号列表")
    @GetMapping("/com/v1/libn")
    public PResult<Set<String>> libn() throws ExecutionException {

        if (cache.get("libn").size() > 0){
            return new PResult<>(cache.get("libn"));
        }

        Set<String> set = new HashSet<String>();

        PPageInfo<TCompanyExh> pageInfo = inspService.query(new TCompanyExhExample(),0, Integer.MAX_VALUE);
        for (TCompanyExh exh : pageInfo.getList()) {
            set.add(exh.getLibNum());
        }

        cache.put("libn", set);

        return new PResult<>(set);
    }
    @ApiOperation("查询条件-馆号列表-通过展区查馆号")
    @GetMapping("/com/v1/libq")
    public PResult<Set<String>> libq(@RequestParam(name= "exh_area",required = true)String exh_area) throws ExecutionException {

        String cacheKey = "libq"+exh_area;

        if (cache.get(cacheKey).size() > 0){
            return new PResult<>(cache.get(cacheKey));
        }

        Set<String> set = new HashSet<String>();
        TCompanyExhExample exhExample = new TCompanyExhExample();
        exhExample.createCriteria().andExhAreaEqualTo(exh_area);

        PPageInfo<TCompanyExh> pageInfo = inspService.query(exhExample, 0, Integer.MAX_VALUE);
        for (TCompanyExh exh : pageInfo.getList()) {
            set.add(exh.getLibNum());
        }

        cache.put(cacheKey, set);

        return new PResult<>(set);
    }
    @ApiOperation("查询条件-期数列表")
    @GetMapping("/com/v1/period")
    public PResult<Set<String>> period() {
        Set<String> set = new HashSet<String>();
        set.add("一期");
        set.add("二期");
        set.add("三期");
        return new PResult<>(set);
    }

    @ApiOperation("查询条件-展区列表")
    @GetMapping("/com/v1/exharea")
    public PResult<Set<String>> exharea() throws ExecutionException {

        if (cache.get("exharea").size() > 0){
            return new PResult<>(cache.get("exharea"));
        }

        Set<String> set = new HashSet<String>();

        PPageInfo<TCompanyExh> pageInfo = inspService.query(new TCompanyExhExample(),0, Integer.MAX_VALUE);
        for (TCompanyExh exh : pageInfo.getList()) {
            set.add(exh.getExhArea());
        }

        cache.put("exharea", set);

        return new PResult<>(set);
    }

    @ApiOperation("查询条件-巡检人列表")
    @GetMapping("/com/v1/patrol")
    public PResult<Set<String>> patrol() throws ExecutionException {

        if (cache.get("patrol").size() > 0){
            return new PResult<>(cache.get("patrol"));
        }

        Set<String> set = new HashSet<String>();

        PPageInfo<TUserInfo> pageInfo = userService.query(new TUserInfoExample(),0, Integer.MAX_VALUE);
        for (TUserInfo user : pageInfo.getList()) {
            set.add(user.getName());
        }

        cache.put("patrol", set);

        return new PResult<>(set);
    }
}
